# _*_ coding:utf-8 _*_
"""
@Version: python3.9
@Project: selenium
@File: demo1.PY
@Date: 2021/8/9 0:08
@Author: jia
@Description: selenium 的使用 示例1
"""

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait


driver = webdriver.Chrome()  # google浏览器驱动
driver.get('http://www.hao123.com')

print(driver.title)
sleep(3)

# 窗口自定义大小
driver.set_window_size(600, 900)
sleep(2)

# 窗口最大化
driver.get('http://www.51zxw.net')
driver.maximize_window()
sleep(2)

# 前进
driver.forward()
sleep(2)
driver.maximize_window()

# 显示等待，
txz_xpath = '//*[@id="swap-currency-output"]/div/div[2]/button'
WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, txz_xpath)))  # 元素出现在bom表中
WebDriverWait(driver, 5).until(EC.visibility_of_element_located((By.XPATH, txz_xpath)))  # 元素出现在屏幕中

# link_text定位
driver.find_element(By.LINK_TEXT, '电脑办公').click()
sleep(2)

# 高级用法：父元素+子元素定位
driver.find_element(By.XPATH, "//div[@id='rank']//a[4]").click()
button = driver.find_element(By.XPATH, "//div[@id='rank']//a[4]")
assert button == 'abc'

# tag_name标签定位
driver.find_elements(By.TAG_NAME, 'input')[2].click()

# 元素组合定位
driver.find_element(By.XPATH, "//input[@class='loinp' and @name='username'] ").send_keys('123')

# class name 定位
driver.find_element(By.CLASS_NAME, 'help').click()
sleep(5)
driver.get('http://www.baidu.com')
driver.find_element(By.NAME, 'wd').send_keys('abc')
driver.find_element(By.ID, 'su').click()
sleep(2)
driver.find_element(By.XPATH, "//div[@id='2']//a[contains(text(),'_')]").click()
driver.find_element(By.ID, 'baidu_translate_input').send_keys('div')
sleep(2)

# 返回
driver.back()
sleep(1)

# 点击控件时，控件背景变红
swap_but = driver.find_element(By.XPATH, '//*[@id="swap-button"]')
driver.execute_script("arguments[0].style.backgroundColor = 'red';", swap_but)
swap_but.click()
# 恢复初始样式
driver.execute_script("arguments[0].setAttribute('style', arguments[1]);", swap_but, "")
sleep(2)

# ID定位，前加 # 号
driver.find_element(By.CSS_SELECTOR, '#kw').send_keys('123')

# autocomplete属性定位 加[ ]号
driver.find_element(By.CSS_SELECTOR, "[autocomplete='off']")

# name属性定位 加[ ]号
driver.find_element(By.CSS_SELECTOR, "[name='wd']").send_keys("selenium")


# 关闭
driver.quit()






